package com.lq.demo.demo.mode.timer.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * @Auther: LQ
 * @Date: 2018/10/15 22:33
 * @Description:
 */
@Slf4j
public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        Date date = new Date();
        log.info("begin time is " + date);
        date.setTime(date.getTime() + 4000);
        Date endDate = new Date();
        endDate.setTime(endDate.getTime() + 10000);

        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myjob", "group1")
                .usingJobData("code", 10010).usingJobData("msg", "jobdetail some  information").build();


       /* Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .usingJobData("triggerkey", "something in trigger")
                .startAt(date)
                .endAt(endDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
*/
      /*  SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .usingJobData("triggerkey", "something in trigger")
                .startAt(date)
                .endAt(endDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
*/
        //秒 分 小时 日 月 周 年
        //18年内每天21：59执行一次
        String expression = "0 59 21 ? * * 2018";
        String expression2 = "* * * * * ?";

        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .usingJobData("triggerkey", "something in trigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(expression2)).build();


        log.info("------------jobdetail names {}", jobDetail.getKey().getName());
        log.info("------------jobdetail groups" + jobDetail.getKey().getGroup());
        log.info("------------jobdetail jobclass" + jobDetail.getJobClass().getName());

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
        Thread.sleep(2000);
        scheduler.standby();
        //挂起3s后继续执行
        Thread.sleep(3000);

        scheduler.start();

        //关闭
        Thread.sleep(3000);
        log.info("state is {}",scheduler.isShutdown());
        scheduler.shutdown(true);

    }
}

